package de.tlabs.ahmad.xml;

import java.io.File; 
import java.io.IOException;
import java.util.Date; 

import javax.swing.text.Document;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Attr;
import org.w3c.dom.Element;

import jxl.*;
import jxl.read.biff.BiffException;


public class excelTOxml 
{
	static int[]num40 = new int[4];
	static int[]num4 = new int[4];
	static org.w3c.dom.Document doc;
	static int j=0;
	static String ExcelFile = "Proband_02.xls";
	static int vp =2;
	
	public static void main(String [ ] args)throws BiffException, IOException, ParserConfigurationException, TransformerException
	{
		Workbook workbook = Workbook.getWorkbook(new File(ExcelFile)); 
		Sheet sheet = workbook.getSheet(0); 
		
		DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
		doc = docBuilder.newDocument();

		Element rootElement = doc.createElement("config");
		
		Attr attr2 = doc.createAttribute("runs");
		attr2.setValue(Integer.toString(1));
		rootElement.setAttributeNode(attr2);
		
		Attr attr1 = doc.createAttribute("blocks");
		attr1.setValue(Integer.toString(2800));
		rootElement.setAttributeNode(attr1);		
		
		Attr attr3 = doc.createAttribute("numbers");
		attr3.setValue(Integer.toString(1));
		rootElement.setAttributeNode(attr3);
		

		
		doc.appendChild(rootElement);

		for(int i= 0;i<2800;)
		{	
			
			if (i%5==0)j++;
			convertLineToXml(sheet,j,rootElement,i+1);
			i=i+5;
		}
		writeToFile(vp);

	}
	
	static int[] StrToInt(String str)
	{
		int number;
		int[] four_num = new int[4];
		number = Integer.parseInt(str);
		for (int i=0;i<4;i++)
		{
			
			four_num[0]= number/1000;
			four_num[1]= (number/100)%10;
			four_num[2]= (number/10)%10;
			four_num[3]= number%10;
		}
		return four_num;
	}
	
	static String readString(Sheet sheet, int row,int column)
	{
		Cell a1 = sheet.getCell(row,column);  
		String stringa1 = a1.getContents();  
		return stringa1;
	}
	
	static int writeToXml(int nr,int fdbck,int number,Element rootElement) throws ParserConfigurationException, TransformerException
	{

	 
		//root elements
		Element block = doc.createElement("block");
		rootElement.appendChild(block);
		
		//set attribute to root element
		Attr attr2 = doc.createAttribute("feedback");
		String k = null;
		if (fdbck == 4)
		{
			k= "multimodal";
		}
		else
		{
			if(fdbck == 3)
			{
				k = "tactile";
			}
			else
			{
				if(fdbck == 2)
				{
					k = "visual";
				}
				else
				{
					if(fdbck == 1)
					{
						k = "auditiv";
					}
					else
					{
						if(fdbck == 5)
						{
							k = "auditivVisual";
						}
						else
						{
							if(fdbck == 6)
							{
								k = "auditivTactile";
							}
							else
							{
								if(fdbck == 7)
								{
									k = "visualTactile";
								}
								else
								{
									if(fdbck == 9)
									{
										k = "noneTrigger";
									}
									else
									{
										if(fdbck == 0)
										{
											k = "none";
										}
										else
											k = "enter";
									}
								}
							}
						}
					}
				}
			}
		}
			
		attr2.setValue(k);
		block.setAttributeNode(attr2);
		
		//set attribute to root element
		Attr attr1 = doc.createAttribute("nr");
		attr1.setValue(Integer.toString(nr));
		block.setAttributeNode(attr1);
		
		Element run = doc.createElement("run");
		
		Attr attr3 = doc.createAttribute("nr");
		attr3.setValue(Integer.toString(1));
		run.setAttributeNode(attr3);
		
		Attr attr4 = doc.createAttribute("sfd");
		attr4.setValue(Integer.toString(0));
		run.setAttributeNode(attr4);
		
		block.appendChild(run);

		Element numbers = doc.createElement("numbers");
		numbers.appendChild(doc.createTextNode(Integer.toString(number)));
		run.appendChild(numbers);
		
		return 0;		
	}
	
	static void convertLineToXml(Sheet sheet,int Line,Element rootElement,int block) throws ParserConfigurationException, TransformerException
	{
		

		String str0= readString(sheet,0,Line);
		System.out.println(""+str0);
			
		num40 = StrToInt(str0);
		System.out.println(""+num40[0]+" "+num40[1]+" "+num40[2]+" "+num40[3]);
			
		String str1= readString(sheet,1,Line);
		System.out.println(""+str1);
			
		num4 = StrToInt(str1);
		System.out.println(""+num4[0]+" "+num4[1]+" "+num4[2]+" "+num4[3]);
		
		if (num4[3]==0)
			num4[3]=9;

		for(int i=0;i<4;i++)
		{			
			writeToXml(block,num4[i],num40[i],rootElement);
			block++;
		}
		writeToXml(block,8,0,rootElement);
	}
	
	static void writeToFile(int vp) throws TransformerException
	{
		//write the content into xml file
		TransformerFactory transformerFactory = TransformerFactory.newInstance();
		Transformer transformer = transformerFactory.newTransformer();
		DOMSource source = new DOMSource(doc);
		StreamResult result =  new StreamResult(new File("config"+vp+".xml"));
		transformer.transform(source, result);
		
		System.out.println("Done");
		
	}
}
